import axios from 'axios';
import baseUrl from '@/uitils/baseUrl';
// 引入pinia
import { mapState } from 'pinia';
import { userStore } from '../store/index'

// 创建Axios实例
const api = axios.create({
	baseURL: baseUrl + '/api',
	timeout: 5000
})
// 请求拦截
api.interceptors.request.use((config: any) => {
	const store = userStore()
	config.headers.Authorization = `Bearer ${store.token}`
	return config
})
// 响应拦截
api.interceptors.response.use(config => {
	// 状态码判断
	if (config.status === 200 || config.status === 201) {
		return config.data
	} else {
		return Promise.reject(new Error(config.data.msg || '请求失败了'))
	}
})
// 导出
export default api